package com.hzeport.bgs22emduty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.dto.ScreenCheckReasonDTO;
import com.hzeport.bgs22emduty.entity.ScreenCheckReason;
import com.hzeport.bgs22emduty.enums.DeleteFlagEnum;
import com.hzeport.bgs22emduty.outbound.ScreenCheckReasonRepository;
import com.hzeport.bgs22emduty.outbound.db.ScreenCheckReasonRepositoryImpl;
import com.hzeport.bgs22emduty.service.ScreenCheckReasonService;
import com.hzeport.bgs22emduty.vo.base.PageVO;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import com.hzeport.bgs22emduty.common.utils.StringUtils;
import com.hzeport.bgs22emduty.common.base.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ScreenCheckReasonServiceImpl extends BaseServiceImpl<ScreenCheckReasonRepositoryImpl, ScreenCheckReason> implements ScreenCheckReasonService {

    @Resource
    private ScreenCheckReasonRepository screenCheckReasonRepository;

    @Override
    public BaseResult<Page<ScreenCheckReason>> list(PageVO dto, UserInfoDetailVO userInfoDetailVO) {
        Page<ScreenCheckReason> page=new Page<>(dto.getPageNum(),dto.getPageSize());
        Page<ScreenCheckReason> screenCheckReasonPage = screenCheckReasonRepository.page(page, new LambdaQueryWrapper<ScreenCheckReason>()
                .eq(ScreenCheckReason::getCustomId, userInfoDetailVO.getCustomscode()));
        return BaseResult.ok(screenCheckReasonPage);
    }

    @Override
    public BaseResult<List<ScreenCheckReason>> pullDownList(UserInfoDetailVO userInfoDetailVO) {
        List<ScreenCheckReason> screenCheckReasonPage = screenCheckReasonRepository.list( new LambdaQueryWrapper<ScreenCheckReason>()
                .eq(ScreenCheckReason::getCustomId, userInfoDetailVO.getCustomscode()));
        return BaseResult.ok(screenCheckReasonPage);
    }

    @Override
    public BaseResult<String> delete(String id, UserInfoDetailVO userInfoDetailVO) {
        if (screenCheckReasonRepository.removeById(id)){
            return BaseResult.ok(null,"删除成功");
        }else {
            return BaseResult.failed("删除失败");
        }
    }

    @Override
    public BaseResult<String> saveOrUpdate(ScreenCheckReasonDTO screenCheckReasonDTO, UserInfoDetailVO userInfoDetailVO) {
        ScreenCheckReason screenCheckReason=new ScreenCheckReason();
        screenCheckReason.setId(screenCheckReasonDTO.getId());
        screenCheckReason.setCustomId(userInfoDetailVO.getCustomscode());
        screenCheckReason.setCreateBy(userInfoDetailVO.getUserId());
        screenCheckReason.setCreateTime(LocalDateTime.now());
        screenCheckReason.setUpdateBy(userInfoDetailVO.getUserId());
        screenCheckReason.setUpdateTime(LocalDateTime.now());
        screenCheckReason.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
        screenCheckReason.setReason(screenCheckReasonDTO.getReason());
        if (StringUtils.isNotEmpty(screenCheckReasonDTO.getId())){
            ScreenCheckReason byId = screenCheckReasonRepository.getById(screenCheckReasonDTO.getId());
            if (byId != null){
                screenCheckReason.setRecVersion(byId.getRecVersion());
            }
        }
        if (screenCheckReasonRepository.saveOrUpdate(screenCheckReason)){
            return BaseResult.ok(null,"操作成功");
        }else {
            return BaseResult.failed("操作失败");
        }
    }
}
